import ConfigParser

########################################################################
class ParseBasicConfig(object):
    """Parse basic config files abstract class. Use ParseModuleConfig to 
    parse whole config with module specific config files. To create new
    section just call the constructor."""

    #----------------------------------------------------------------------
    def __init__(self, file, section):
        """Constructor. Sets up config params in config file (or creates new one)."""
        self.file = file
        self.section = section
        
        config = ConfigParser.RawConfigParser()
        self.config = config
        config.read(file)
        
        if not config.has_section(section):
            config.add_section(section)
        
        if not config.has_option(section, 'module'):
            config.set(section, 'module', '')
        self.module = config.get(section, 'module')
        
        if not config.has_option(section, 'is_encrypted'):
            config.set(section, 'is_encrypted', 'False')
        self.encrypted = config.getboolean(section, 'is_encrypted')
        
        if config.has_option(section, 'is_encrypted') and config.getboolean(section, 'is_encrypted'):
            self.encryptionKey = config.get(section, 'encryption_key_path')
        
        if not config.has_option(section, 'backup_path'):
            config.set(section, 'backup_path', '')
        self.backupPath = config.get(section, 'backup_path')
        
        if not config.has_option(section, 'backup_excludes_path'):
            config.set(section, 'backup_excludes_path', '')
        self.backupExcludesPath = config.get(section, 'backup_excludes_path')

        if not config.has_option(section, 'output_file_suffix'):
            config.set(section, 'output_file_suffix', 'ebackuper_backup')
        self.suffix = config.get(section, 'output_file_suffix')
        
        if not config.has_option(section, 'post_command'):
            config.set(section, 'post_command', '')
        self.postCommand = config.get(section, 'post_command')

        if not config.has_option(section, 'pre_command'):
            config.set(section, 'pre_command', '')
        self.preCommand = config.get(section, 'pre_command')        

        if not config.has_option(section, 'postfail_command'):
            config.set(section, 'postfail_command', '')
        self.postFailCommand = config.get(section, 'postfail_command')


        if not config.has_option(section, 'preserve_count'):
            config.set(section, 'preserve_count', '-1')
        self.preserveCount = config.getint(section, 'preserve_count')        
        
        
        
        self.saveChanges()
        
    #----------------------------------------------------------------------
    # getters
    #----------------------------------------------------------------------
    def getSectionName(self):
        """Returns a section name"""
        return self.section
    
    #----------------------------------------------------------------------
    def getModuleName(self):
        """Returns a module name"""
        return self.module
    
    #----------------------------------------------------------------------
    def isEncrypted(self):
        """Returns bool: true if encryption is used and false otherwise"""
        return self.encrypted
    
    #----------------------------------------------------------------------
    def getEncryptionKey(self):
        """Returns the key path used for encryption"""
        return self.encryptionKey
    
    #----------------------------------------------------------------------
    def getBackupPath(self):
        """Returns file path with backup paths"""
        return self.backupPath
    
    #----------------------------------------------------------------------
    def getBackupExcludesPath(self):
        """Returns file path with backup exclude paths"""
        return self.backupExcludesPath
    
    #----------------------------------------------------------------------
    def getOutputFileSuffix(self):
        """Returns output filename suffix"""
        return self.suffix
    
    #----------------------------------------------------------------------
    def getPostCommand(self):
        """Returns post process command"""
        return self.postCommand
     
    #----------------------------------------------------------------------
    def getPostFailCommand(self):
        """Returns post fail process command"""
        return self.postFailCommand
    
    #----------------------------------------------------------------------
    def getPreCommand(self):
        """Returnes pre process command"""
        return self.preCommand

    #----------------------------------------------------------------------
    def getPreserveCount(self):
        """Returns int containing how many backups are stored.
         n > 0 - preserve n backups.
         Old backups are deleted after new is created.
         So n = 1 means that only current backups are kept."""
        return self.preserveCount
    #----------------------------------------------------------------------
    # saver
    #----------------------------------------------------------------------
    def saveChanges(self):
        """Saves config"""
        with open(self.file, 'wb') as configfile:
            self.config.write(configfile)
    
    #----------------------------------------------------------------------
    # setters
    #----------------------------------------------------------------------
    def setModuleName(self, module):
        """Sets a module name"""
        self.module = module
        self.config.set(self.section, "module", module)
    
    #----------------------------------------------------------------------
    def setEncrypted(self, encrypted):
        """True if encryption is used and false otherwise"""
        self.encrypted = encrypted
        self.config.set(self.section, "is_encrypted", encrypted)
    
    #----------------------------------------------------------------------
    def setEncryptionKey(self, encryptionKey):
        """Sets file key used for encryption"""
        self.encryptionKey = encryptionKey
        self.config.set(self.section, "encryption_key_path", encryptionKey)
    
    #----------------------------------------------------------------------
    def setBackupPath(self, backupPath):
        """Sets file with backup path"""
        self.backupPath = backupPath
        self.config.set(self.section, "backup_path", backupPath)
    
    #----------------------------------------------------------------------
    def setBackupExcludesPath(self, backupExcludesPath):
        """Sets file with backup exclude path"""
        self.backupExcludesPath = backupExcludesPath
        self.config.set(self.section, "backup_excludes_path", backupExcludesPath)
    
    #----------------------------------------------------------------------
    def setOutputFileSuffix(self, suffix):
        """Returns output filename suffix"""
        self.suffix = suffix
        self.config.set(self.section, "output_file_suffix", suffix)
    
    #----------------------------------------------------------------------
    def setPostCommand(self, postCommand):
        """Returns post process command"""
        self.postCommand = postCommand
        self.config.set(self.section, "post_command", postCommand)
 
    #----------------------------------------------------------------------
    def setPostFailCommand(self, postFailCommand):
        """Returns post fail process command"""
        self.postFailCommand = postFailCommand
        self.config.set(self.section, "postfail_command", postFailCommand)

    #----------------------------------------------------------------------
    def setPreCommand(self, preCommand):
        """Returns pre process command"""
        self.preCommand = preCommand
        self.config.set(self.section, "pre_command", preCommand)        
        
    #----------------------------------------------------------------------
    def setPreserveCount(self, preserveCount):
        """Sets how many backups to preserve"""
        self.preserveCount = preserveCount
        self.config.set(self.section, "preserve_count", preserveCount)